﻿using System.Data;
using Microsoft.SharePoint.Search.Query;

namespace QueryDemo.ResultProviders
{
	/// <summary>
	/// Full Text SQL for Windows SharePoint Services Search.
	/// </summary>
	public class FullTextSqlWssResultProvider : BaseResultProvider
	{
		private FullTextSqlQuery _fullTextSqlQuery;

		/// <summary>
		/// Sets properties related to use of the query within the web part.
		/// </summary>
		public FullTextSqlWssResultProvider()
		{
			//TypeOfQuery = QueryType.FullTextWssSqlQuery;
			QueryName = "Full Text SQL (WSS)";
		}

		public override bool CrossSiteCapable
		{
			get { return true; }
		}

		public override void Init()
		{
			string query = "SELECT Path, Title, Write, Created " +
						   "FROM portal..scope() " +
						   "WHERE (\"SCOPE\" = 'All Sites')";

			_fullTextSqlQuery = new FullTextSqlQuery(Site)
			                    	{
			                    		QueryText = query,
			                    		ResultTypes = ResultType.RelevantResults,
			                    		RowLimit = RowLimit
			                    	};
		}

		public override DataTable ExecuteQuery()
		{
			var resultsDataTable = new DataTable();
			var resultsCollection = _fullTextSqlQuery.Execute();
			if (resultsCollection.Count > 0)
			{
				var resultTable = resultsCollection[ResultType.RelevantResults];
				resultsDataTable.Load(resultTable);
			}
			return resultsDataTable;
		}
	}
}
